package com.srzk.youthleagueback.service;

import com.srzk.youthleagueback.entity.po.Student;
import com.srzk.youthleagueback.mapper.StudentMapper;
import com.srzk.youthleagueback.results.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.srzk.youthleagueback.results.CodeMsg.*;

@Service
public class StudentService {
    @Autowired
    StudentMapper studentMapper;

    public Result<Student> get(String userName) {
        Student user = studentMapper.selectByPrimaryKey(userName);
        if(user==null) {
            return Result.error(NOT_EXIST);
        } else {
            return Result.success(user);
        }
    }

    public Result<List<Student>>  getList(String cla,String dname,String grade){
        List<Student> user = studentMapper.selectByclass(cla,dname,grade);
        if(user==null||user.isEmpty()){
            return Result.error(NOT_EXIST);
        }else{
            return Result.success(user);
        }
    }

    public Result<List<Student>> getAllList(){
        List<Student> user = studentMapper.selectAll();
        if(user==null||user.isEmpty()){
            return Result.error(NOT_EXIST);
        }else{
            return Result.success(user);
        }
    }

    public Result<Student> updateStudent(Student student){
        int result = studentMapper.updateByPrimaryKey(student);
        if (result==1){
            return Result.success(student);
        }else{
            return Result.error(UPDATE_FAIL);
        }
    }

    public Result<String> delete(String student){
        int result = studentMapper.deleteByUsername(student);
        if(result==1){
            return Result.success("删除成功");
        }else{
            return Result.error(DELETE_FAIL);
        }
    }

    public Result<String> add(Student student){
        int result = studentMapper.insert(student);
        if(result==1){
            return Result.success("success");
        }else{
            return Result.error(ADD_FAIL);
        }
    }
}
